package com.afu.demo;

import cn.hutool.core.date.StopWatch;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * ClassName: Demo4
 * Package: com.afu
 * Description:
 *  arrayList和linkList随机写入耗时对比
 *
 * @Author zxf
 * @Create 2024/2/6 15:23
 * @Version 1.0
 */
public class Demo4 {
    public static void main(String[] args) {
        List<Integer> intArrayList = new ArrayList<>();
        List<Integer> intLinkedList = new LinkedList<>();

        StopWatch stopWatch = new StopWatch();

        stopWatch.start("linkList");
        arrayWrite(intLinkedList);
        stopWatch.stop();

        stopWatch.start("arrayList");
        arrayWrite(intArrayList);
        stopWatch.stop();

        System.out.println(stopWatch.prettyPrint());
    }

    public static void arrayWrite(List<Integer> list){
        Random random = new Random();
        for (int i = 0; i < 100000; i++) {
            int randomIndex = random.nextInt(100000);
            list.add(randomIndex);
        }
    }
}
